package repository

import (
	"buding-ai/internal/core"
	"buding-ai/internal/repository/model"
	"buding-ai/internal/web/response"
	"buding-ai/pkg/utils/paginate"
	"gorm.io/gorm"
	"sync"
)

var (
	agentModelProviderRepos *AgentModelProviderRepository
	agentModelProviderOnce  sync.Once
)

type AgentModelProviderRepository struct {
	db *gorm.DB
}

func GetAgentModelProviderRepository() *AgentModelProviderRepository {
	agentModelProviderOnce.Do(func() {
		agentModelProviderRepos = &AgentModelProviderRepository{
			db: core.DB,
		}
	})
	return agentModelProviderRepos
}

func (r *AgentModelProviderRepository) CheckProviderExistsByName(name string) (bool, error) {
	var count int64
	if err := r.db.Model(&model.AgentModelProvider{}).Where("name = ?", name).Count(&count).Error; err != nil {
		return false, err
	}
	return count > 0, nil
}

func (r *AgentModelProviderRepository) Create(entity model.AgentModelProvider) error {
	return r.db.Create(&entity).Error
}

func (r *AgentModelProviderRepository) GetById(id uint) (*model.AgentModelProvider, error) {
	var provider model.AgentModelProvider
	if err := r.db.First(&provider, id).Error; err != nil {
		return nil, err
	}
	return &provider, nil
}

func (r *AgentModelProviderRepository) DeleteById(id uint) error {
	return r.db.Delete(&model.AgentModelProvider{}, id).Error
}

func (r *AgentModelProviderRepository) Update(entity *model.AgentModelProvider) error {
	return r.db.Save(&entity).Error
}

func (r *AgentModelProviderRepository) SetDefault(id uint) error {
	// 先取消所有默认设置
	if err := r.db.Model(&model.AgentModelProvider{}).Where("is_default = ?", true).Update("is_default", false).Error; err != nil {
		return err
	}
	// 设置新的默认提供商
	return r.db.Model(&model.AgentModelProvider{}).Where("id = ?", id).Update("is_default", true).Error
}

func (r *AgentModelProviderRepository) GetPage(pageRequest paginate.PageRequest) (*paginate.PageResponse, error) {
	query := core.DB.Model(&model.AgentModelProvider{})

	// 如果有搜索关键词，添加模糊查询条件
	if pageRequest.Keyword != "" {
		searchTerm := "%" + pageRequest.Keyword + "%"
		query = query.Where("name LIKE ? OR description LIKE ?", searchTerm, searchTerm)
	}

	// 执行分页查询
	var providers []response.AgentModelProviderInfoVO
	pageResult, err := paginate.Paginator(query, pageRequest.Page, pageRequest.PageSize, pageRequest.Sort, pageRequest.Order, &providers)

	if err != nil {
		return nil, err
	}

	return &pageResult, nil
}

func (r *AgentModelProviderRepository) GetDefaultProvider() (*model.AgentModelProvider, error) {
	var provider model.AgentModelProvider
	if err := r.db.Where("is_default = ? AND status = ?", true, 1).First(&provider).Error; err != nil {
		return nil, err
	}
	return &provider, nil
}

func (r *AgentModelProviderRepository) GetActiveProviders() ([]model.AgentModelProvider, error) {
	var providers []model.AgentModelProvider
	if err := r.db.Where("status = ?", 1).
		Order("priority ASC, created_at DESC").Find(&providers).Error; err != nil {
		return nil, err
	}
	return providers, nil
}
